﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;

namespace msBsc.AppLogic.DataAlc
{
    /// <summary>
    /// 统计 页面访问信息
    /// </summary>
    /// Author:蒲崇彬
    /// Creation date:2015/07/28 10:09
    public class CBizCustomerViews : CustBizObject
    {
        private CDmDjkCustomerViews m_CDmDjkCustomerViews = new CDmDjkCustomerViews();
        private CDmDjkCustomerViews oDmDjkCustomerViews
        {
            get { return m_CDmDjkCustomerViews; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizCustomerViews()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkCustomerViews.DJK_CUSTOMER_VIEWS);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["CUSTOMER_VIEW_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        //public DataSet SelectByIds(string strIdS)
        //{
        //    CDbParmCollection parmCollection = new CDbParmCollection();
        //    parmCollection.Add(new CDbParm("strIdS", ParameterDirection.Input, EDbType.String, 36, strIdS.Replace("'", "").Replace(",", "','")));
        //    DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE  " + this.KeyFieldName + " IN  ('@strIdS')");
        //    ds.Tables[0].TableName = this.TableName;
        //    return ds;
        //}



        /// <summary>
        /// 用户浏览记录
        /// </summary>
        /// <param name="pageName">页面名称</param>
        /// <returns></returns>
        public string AddCustViewRecord(string pageName)
        {
            if (string.IsNullOrEmpty(pageName))
            {
                return "浏览页面名称不能为空";
            }
            DataSet ds = new CDmDjkCustomerViews();
            DataRow dr = ds.Tables[0].NewRow();

            dr["CUSTOMER_VIEW_PAGE"] = pageName;

            ds.Tables[0].Rows.Add(dr);

            this.Update(ds);

            return "";
        }

        /// <summary>
        /// 根据时间获取页面点击信息
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="stopTime">结束时间</param>
        /// <param name="pageName">页面名称</param>
        /// <returns></returns>
        public DataSet GetClickInfoByTime(string startTime, string stopTime, string pageName)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();

            parmCollection.Add(new CDbParm("starttime", ParameterDirection.Input, EDbType.String, 100, startTime.Replace(",", "").Replace(",", "','")));
            parmCollection.Add(new CDbParm("stoptime", ParameterDirection.Input, EDbType.String, 100, stopTime.Replace(",", "").Replace(",", "','")));
            parmCollection.Add(new CDbParm("pagename", ParameterDirection.Input, EDbType.String, 100, pageName.Replace(",", "").Replace(",", "','")));

            DataSet ds = this.DataObj.QueryBySP("DJK_SELECT_CLICK_COUNT_FOR_VIEW", parmCollection);

            return ds;
        }

        /// <summary>
        /// 添加浏览记录
        /// </summary>
        /// <param name="invitaCode"></param>
        /// <param name="pageName"></param>
        public void AddCustomerViewRecord(string invitaCode, string pageName)
        {
            DataSet ds = new CDmDjkCustomerViews();

            DataRow dr = ds.Tables[0].NewRow();
            dr["CUSTOMER_VIEW_ID"] = this.DataObj.NewID();
            dr["CUSTOMER_VIEW_INCODE"] = invitaCode;
            dr["CUSTOMER_VIEW_PAGE"] = pageName;
            dr["CUSTOMER_VIEW_TIME"] = this.dtCurrDateTime;
            dr["CUSTOMER_VIEW_IP"] = msBsc.Cls.CommValiCollection.GetIp();
            dr["CUSTOMER_VIEW_INCODE"] = invitaCode;
            dr["VIEW_IS_DELETED"] = 0;
            dr["VIEW_IS_DISPLAY"] = 1;

            ds.Tables[0].Rows.Add(dr);
            this.Update(ds);
        }

        /// <summary>
        /// 流量记录添加
        /// </summary>
        /// <param name="pageCode">页面编码</param>
        /// <param name="pageName">页面名称</param>
        /// <param name="pageUrl">页面地址</param>
        /// <param name="userId">用户id</param>
        /// <param name="countryid">国家区域 id</param>
        /// <param name="cityid">城市 id</param>
        /// <param name="destiantionid">地区id</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015/07/28 14:40
        public string AddCustomerViewRecord(string pageCode, string pageName, string pageUrl, string userId, string countryid, string cityid, string destiantionid)
        {
            string ip = msBsc.Cls.CommValiCollection.GetIp();
            DataSet ds = new CDmDjkCustomerViews();
            DataRow dr = ds.Tables[0].NewRow();
            dr["CUSTOMER_VIEW_ID"] = this.DataObj.NewID();

            dr["CUSTOMER_PAGE_ENCODE"] = pageCode;
            dr["CUSTOMER_VIEW_PAGE"] = pageName;
            dr["CUSTOMER_ID"] = userId;
            dr["VIEW_URL"] = pageUrl;
            dr["CUSTOMER_VIEW_TIME"] = this.dtCurrDateTime;
            dr["CUSTOMER_VIEW_IP"] = ip;

            if (ip != "127.0.0.1" && ip != "::1" && !string.IsNullOrEmpty(ip))
            {
                //地区
                Cls.AreaIpInfo areaInfo = msBsc.Cls.HttpWebHelper.HttpGetAreaInfo(ip);
                if (areaInfo != null && areaInfo.code == "0")
                {
                    //国家
                    dr["COUNTRY_NAME"] = areaInfo.data.country;
                    //省
                    dr["PROVINCE_NAME"] = areaInfo.data.region;
                    //市
                    dr["CITY_NAME"] = areaInfo.data.city;
                    //区
                    dr["AREA_NAME"] = areaInfo.data.county;
                }
            }
            dr["VEHICLE_COUNTRY"] = countryid;
            dr["VEHICLE_CITY"] = cityid;
            dr["VEHICLE_DESTINATION"] = destiantionid;
            dr["VEHICLE_COUNTY"] = "";

            dr["VIEW_IS_DELETED"] = 0;
            dr["VIEW_IS_DISPLAY"] = 1;
            ds.Tables[0].Rows.Add(dr);
            this.Update(ds);

            return "OK";
        }


        /// <summary>
        /// 查询访问记录信息
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="stopTime">结束时间</param>
        /// <param name="dataType">请求的类型</param>
        /// <returns>查询的结果</returns>
        /// Author:刘魏
        /// Creation date:2015/08/03 14:50
        public DataSet GetCityCustomerViewInfo(string startTime, string stopTime, string dataType)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("starttime", ParameterDirection.Input, EDbType.String, 10, startTime));
            parmCollection.Add(new CDbParm("stoptime", ParameterDirection.Input, EDbType.String, 10, stopTime));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT T.*
	,B.COUNTRY_CITY_NME S_COUNTRY
	,C.COUNTRY_CITY_NME S_CITY
FROM (SELECT count(1) VIEW_NUM,A.COUNTRY_NAME,A.PROVINCE_NAME,A.CITY_NAME,A.VEHICLE_COUNTRY,A.VEHICLE_CITY,A.CUSTOMER_VIEW_PAGE FROM DJK_CUSTOMER_VIEWS A
WHERE (@starttime = '' OR A.CUSTOMER_VIEW_TIME >= @starttime) AND (@stoptime = '' OR A.CUSTOMER_VIEW_TIME <= @stoptime)
GROUP BY A.COUNTRY_NAME,A.PROVINCE_NAME,A.CITY_NAME,A.VEHICLE_COUNTRY,A.VEHICLE_CITY,A.CUSTOMER_VIEW_PAGE
HAVING A.COUNTRY_NAME<>'' AND A.COUNTRY_NAME IS NOT NULL 
	AND A.VEHICLE_COUNTRY<>'' AND A.VEHICLE_COUNTRY<>'undefined'
	AND A.VEHICLE_CITY<>'' AND A.VEHICLE_CITY<>'undefined') T
LEFT JOIN DJK_COUNTRY_CITY B ON T.VEHICLE_COUNTRY=B.COUNTRY_CITY_ID
LEFT JOIN DJK_COUNTRY_CITY C ON T.VEHICLE_CITY=C.COUNTRY_CITY_ID", parmCollection);

            return ds;
        }
    }
}
